package org.jeecg.modules.website.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.system.service.ISysDictService;
import org.jeecg.modules.website.entity.WebSchoolMessage;
import org.jeecg.modules.website.entity.WebWorkFile;
import org.jeecg.modules.website.mapper.WebSchoolMessageMapper;
import org.jeecg.modules.website.mapper.WebWorkFileMapper;
import org.jeecg.modules.website.service.IWebSchoolMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 学校信息表
 * @Author: jeecg-boot
 * @Date:   2024-08-02
 * @Version: V1.0
 */
@Service
public class WebSchoolMessageServiceImpl extends ServiceImpl<WebSchoolMessageMapper, WebSchoolMessage> implements IWebSchoolMessageService {


    @Autowired
    private WebSchoolMessageMapper webSchoolMessageMapper;
    @Autowired
    private WebWorkFileMapper webWorkFileMapper;
    @Autowired
    private ISysDictService sysDictService;

    public String getDictDescByCategoryAndType(String category, String type) {
        // 根据 category 确定字典编码
        // String dictCode = determineDictCodeByCategory(category);
        // 查询字典项
        String dictItem = sysDictService.queryDictTextByKey(category, type);
        return dictItem ;
    }
    public void saveMain(WebSchoolMessage webSchoolMessage, List<WebWorkFile> webWorkFileList) {
        webSchoolMessageMapper.insert(webSchoolMessage);
        if(webWorkFileList!=null && webWorkFileList.size()>0) {
            for(WebWorkFile entity:webWorkFileList) {
                //外键设置
                entity.setGuideId(webSchoolMessage.getId());
                webWorkFileMapper.insert(entity);
            }
        }
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMain(WebSchoolMessage webSchoolMessage,List<WebWorkFile> webWorkFileList) {
        webSchoolMessageMapper.updateById(webSchoolMessage);

        //1.先删除子表数据
        webWorkFileMapper.deleteByMainId(webSchoolMessage.getId());

        //2.子表数据重新插入
        if(webWorkFileList!=null && webWorkFileList.size()>0) {
            for(WebWorkFile entity:webWorkFileList) {
                //外键设置
                entity.setGuideId(webSchoolMessage.getId());
                webWorkFileMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delMain(String id) {
        webWorkFileMapper.deleteByMainId(id);
        webSchoolMessageMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for(Serializable id:idList) {
            webWorkFileMapper.deleteByMainId(id.toString());
            webSchoolMessageMapper.deleteById(id);
        }
    }
    @Override
    public List<WebSchoolMessage> selectMajorList() {
        return  webSchoolMessageMapper.selectMajorList();

    }
    @Override
    public List<WebSchoolMessage> selectMajor() {
        return  webSchoolMessageMapper.selectMajor();

    }

    @Override
    public WebSchoolMessage selectMajorName(String majorName) {
        return  webSchoolMessageMapper.selectMajorName(majorName);

    }


}
